package com.education.module.system.sysUserRole;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.education.domain.SysRole;
import com.education.domain.SysUserRole;
import com.education.domain.User;
import com.education.framework.base.BaseController;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.framework.session.SessionHelper;
import com.education.framework.util.calendar.CalendarUtil;
import com.education.module.system.sysRole.SysRoleServices;

@Controller
@RequestMapping("system/sysUserRole")
public class SysUserRoleController extends BaseController{

	@Autowired
	private SysUserRoleServices services;
	@Autowired
	private SysRoleServices roleServices;
	
	@RequestMapping(value = "setUserRole/{userid}")
	public String setUserRole(Model model, SearchParams searchParams,Page page,ServletRequest request,@PathVariable("userid") Integer userid){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userid", userid);
		searchParams = new SearchParams();
		searchParams.setMap(map);
		List<SysUserRole> list = services.find(searchParams,page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		model.addAttribute("userid", userid);
		model.addAttribute("searchParams", searchParams);
		return "/system/sysUserRole/sysUserRoleList";
	}
	
	@RequestMapping(value = "create/{userid}", method = RequestMethod.GET)
	public String createForm(Model model,@PathVariable("userid") Integer userid) {
		List<SysRole> roleList = roleServices.find();
		model.addAttribute("action","create");
		model.addAttribute("userid",userid);
		model.addAttribute("roleList",roleList);
		return "/system/sysUserRole/sysUserRoleEdit";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(SysUserRole sysUserRole, Model model,RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		sysUserRole.setCreateUser(sessionUser);
		sysUserRole.setUpdateUser(sessionUser);
		sysUserRole.setCreateTime(CalendarUtil.getCurrentDate());
		services.save(sysUserRole);
		redirectAttributes.addFlashAttribute("sysUserRole", "SUCCESS");
		return "redirect:/system/sysUserRole/setUserRole/"+sysUserRole.getUserId();
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Integer id, Model model) {
		SysUserRole sysUserRole = services.findForObject(id);
		model.addAttribute("sysUserRole", sysUserRole);
		model.addAttribute("action", "update");
		return "/system/sysUserRole/sysUserRoleEdit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(SysUserRole sysUserRole, RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		sysUserRole.setUpdateUser(sessionUser);
		services.update(sysUserRole);
		redirectAttributes.addFlashAttribute("sysUserRole", "SUCCESS");
		return "redirect:/system/sysUserRole";
	}
	
	@RequestMapping(value = "delete/{id}/{userid}")
	public String delete(@PathVariable("id") Integer id,@PathVariable("userid") Integer userid,RedirectAttributes redirectAttributes) {
		services.delete(id);
		redirectAttributes.addFlashAttribute("sysUserRole", "SUCCESS");
		return "redirect:/system/sysUserRole/setUserRole/"+userid;
	}
	
}
